package at.jku.ssw.fortress.ui;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.ui.editors.text.FileDocumentProvider;

public class DocumentProvider extends FileDocumentProvider {
	
	@Override
	protected IDocument createDocument(Object element) throws CoreException {
		IDocument doc = super.createDocument(element);
		if(doc != null) {
			FastPartitioner fp = new FastPartitioner(createPartitioner(),
					new String[] { ContentTypes.COMMENT, ContentTypes.STRING });
			fp.connect(doc);
			doc.setDocumentPartitioner(fp);
		}
		return doc;
	}

	private IPartitionTokenScanner createPartitioner() {
		Token comment = new Token(ContentTypes.COMMENT);
		Token string = new Token(ContentTypes.STRING);
		RuleBasedPartitionScanner rbs = new RuleBasedPartitionScanner();
		IPredicateRule[] rules = new IPredicateRule[2];
		rules[0] = new SingleLineRule("\"", "\"", string);
		rules[1] = new MultiLineRule("(*", "*)", comment);
		rbs.setPredicateRules(rules);
		return rbs;
	}

}
